package com.darren.euc.action;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.darren.euc.entity.ContractTemplate;
import com.darren.euc.entity.PageBean;
import com.darren.euc.entity.ResponseJSON;
import com.darren.euc.entity.User;
import com.darren.euc.service.ContractTemplateService;
import com.darren.euc.util.PropertyHelper;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/contracttemplate")
public class ContractTemplateAction {
	
	private static final Logger log = Logger.getLogger(ContractTemplateAction.class);
	
	@Autowired
    private ContractTemplateService contractTemplateService;
	
	
	@ResponseBody
    @RequestMapping(value = "/list")//,method = RequestMethod.POST)
    public List<ContractTemplate> getAllFunctions(HttpServletRequest request,HttpServletResponse response) {
    	List<ContractTemplate> contractTemplates = contractTemplateService.getAll();    	
        return contractTemplates;
    }
	
	@ResponseBody
    @RequestMapping(value = "/enables",method = RequestMethod.POST)
    public List<ContractTemplate> getAllEnable(HttpServletRequest request,HttpServletResponse response) {
    	List<ContractTemplate> contractTemplates = contractTemplateService.getAllEnable();    	
        return contractTemplates;
    }
	
	@SuppressWarnings("rawtypes")
	@ResponseBody
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public ResponseJSON queryContractTemplate(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		PageBean pageBean = contractTemplateService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
		responseJson.setData(pageBean);
		return responseJson;
    }

	
	@ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResponseJSON saveContractTemplate(@RequestBody ContractTemplate contractTemplate,HttpServletRequest request,HttpServletResponse response) {	
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if(user != null){
				contractTemplate.setCreatedBy(user.getId().toString());
				contractTemplate.setLastModifyBy(user.getId().toString());
			}
			contractTemplate.setCreateTime(new Date());
			contractTemplate.setLastModifyTime(new Date());
			contractTemplate = contractTemplateService.save(contractTemplate);
			responseJson.setStatus(0);
			responseJson.setMessage("添加成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("添加失败！");
		}
		responseJson.setData(contractTemplate);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
    }
	
	@ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public ResponseJSON deleteRole(@RequestBody JSONObject requestJson,HttpServletRequest request,HttpServletResponse response) {
    	ResponseJSON responseJson = new ResponseJSON();
    	JSONObject jsonData = new JSONObject();
    	responseJson.setData(jsonData);
    	try{
    		Integer id = Integer.parseInt(requestJson.getString("id"));
    		ContractTemplate contractTemplate = contractTemplateService.findById(id);
    		contractTemplate.setEnable(false);
    		contractTemplate.setStatus(2);
        	responseJson.setStatus(0);
    		responseJson.setMessage("删除成功！");
    	}catch(Exception ex){
    		log.info(ex.getMessage(),ex);
        	responseJson.setStatus(-1);
    		responseJson.setMessage("删除失败！");
    	}
    	System.out.println("==============================================================");
    	System.out.println(requestJson.toString());
    	System.out.println(responseJson.toString());
    	System.out.println("==============================================================");
        return responseJson;
    }
	  
	@ResponseBody
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ResponseJSON updateContractTemplate(@RequestBody ContractTemplate contractTemplate, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			User user = (User) request.getAttribute("user");
			if(user != null){
				contractTemplate.setLastModifyBy(user.getId().toString());
			}
			contractTemplate.setLastModifyTime(new Date());
			contractTemplate = contractTemplateService.update(contractTemplate);			
			responseJson.setStatus(0);
			responseJson.setMessage("保存成功！");
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("保存失败！");
		}
		responseJson.setData(contractTemplate);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
	
	@ResponseBody
	@RequestMapping(value = "/enable", method = RequestMethod.POST)
	public ResponseJSON updateEnableContractTemplate(@RequestBody ContractTemplate contractTemplate, HttpServletRequest request,
			HttpServletResponse response) {
		ResponseJSON responseJson = new ResponseJSON();
		try {
			
			List<ContractTemplate> contractTemplates = contractTemplateService.getEnableContractTemplateBySpaceIdNotEquelsId(contractTemplate.getSpaceId(),contractTemplate.getId(),contractTemplate.getContractType());
			if(contractTemplates != null && contractTemplates.size() > 0){
				responseJson.setStatus(0);
				responseJson.setMessage("请先停用该空间已经启用的其它模板！");
			}else{
				User user = (User) request.getAttribute("user");
				if(user != null){
					contractTemplate.setLastModifyBy(user.getId().toString());
				}
				contractTemplate.setLastModifyTime(new Date());
				contractTemplate = contractTemplateService.update(contractTemplate);			
				responseJson.setStatus(0);
				responseJson.setMessage("启用成功！");
			}
			
		} catch (Exception ex) {
			log.info(ex.getMessage(), ex);
			responseJson.setStatus(-1);
			responseJson.setMessage("启用失败！");
		}
		responseJson.setData(contractTemplate);
		System.out.println("==============================================================");
		System.out.println(responseJson.toString());
		System.out.println("==============================================================");
		return responseJson;
	}
	
}